package com.ymatou.iapi.topicrecbizer.testcase;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.print.DocFlavor.STRING;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.topicrecbizer.parameter.ProductBean;
import com.ymatou.iapi.topicrecbizer.service.ProductCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;

public class Ts_Product {
	private static ProductBean productBean;
	private static ProductCall productCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商品Topic推荐前台接口");
	}

	@Before
	public void caseUp() {
		productBean = new ProductBean();
		productCall = new ProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(productCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Product_001() {
		Logger.start(true,"商品Topic推荐前台接口-检查接口返回公共信息 ");
		try {
			//设置参数
			String requestid="111111";
			String score = "tpscore";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			productBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			productBean.setSort("desc("+score+")");
			productBean.setAppkey("1");
			productBean.setRequestid(requestid);
			productBean.setModule("homepage_topic_product");
			productBean.setPlatform("appbuyer");
			productBean.setLimit("0,10");
			
			productCall.setData(productBean);
			productCall.callService();
			
			Logger.verifyEquals("OK",productCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(productCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, productCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, productCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyEquals(requestid, productCall.getCommonInfo("requestid"), "验证requestid值是否为所传值");
			Logger.verifyNotNull(productCall.getCommonInfo("hostip"), "验证是否有返回服务器ip");
			Logger.verifyEquals(score, productCall.getCommonInfo("algorithm"), "验证返回排序方式是否与所传值相同");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Product_002() {
		Logger.start(true,"商品Topic推荐前台接口-检查接口返回数据，确保不会重复 ");
		try {
			//设置参数
			String requestid="111111";
			String score = "tpscore";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			productBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			productBean.setSort("desc("+score+")");
			productBean.setAppkey("1");
			productBean.setRequestid(requestid);
			productBean.setModule("homepage_topic_product");
			productBean.setPlatform("appbuyer");
			productBean.setLimit("0,10");
			
			productCall.setData(productBean);
			productCall.callService();
			
			Logger.verifyEquals("OK", productCall.getStatus(), "验证接口返回状态是否正常");
			JSONArray hitsAry = productCall.getHits();
			
			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String id = json.getString("id");
				String buyReason =json.getString("buyReason");
				
				if (list.contains(id) == true) {

					Logger.verifyEquals(false, list.contains(id), "验证第" + i+ "条返回数据的id存在重复值" + id);
				//	Logger.verifyEquals(false, list.contains(buyReason), "验证第" + i+ "条返回数据的showTitle存在重复值" + buyReason);

				}else{
					Logger.verifyEquals(false, list.contains(id), "验证第" + i+ "条返回数据的id不存在重复值" + id);
					//Logger.verifyEquals(false, list.contains(buyReason), "验证第" + i+ "条返回数据的showTitle存在重复值" + buyReason);

					list.add(id);
					list.add(buyReason);
				}
			
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Product_003() {
		Logger.start(true,"商品Topic推荐前台接口-检查接口每条数据的返回值是否齐全，并且有有效值 ");
		try {
			//设置参数
			String requestid="111111";
			String score = "tpscore";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			productBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			productBean.setSort("desc("+score+")");
			productBean.setAppkey("1");
			productBean.setRequestid(requestid);
			productBean.setModule("homepage_topic_product");
			productBean.setPlatform("appbuyer");
			productBean.setLimit("0,10");
			
			productCall.setData(productBean);
			productCall.callService();
			
			Logger.verifyEquals("OK", productCall.getStatus(), "验证接口返回状态是否正常");
			
			JSONArray hitsAry = productCall.getHits();
			
			for(int i=1;i<=hitsAry.length();i++){
				
			 String info = hitsAry.get(i-1).toString();
			 JSONObject infoAry = new JSONObject(info);
			 
			//获取接口返回的key校验返回字段
			  Logger.verifyEquals(true,infoAry.has("id") , "验证第"+i+"条记录返回结果中有id这个key");
			  Logger.verifyEquals(true,infoAry.has("buyReason") , "验证第"+i+"条记录返回结果中有buyReason这个key");
			  Logger.verifyEquals(true, infoAry.has("picUrl"), "验证第"+i+"条记录返回结果中有picUrl这个key");
			  Logger.verifyEquals(true, infoAry.has("productIds"), "验证第"+i+"条记录返回结果中有productIds这个key");
			  Logger.verifyEquals(true,infoAry.has("stockNum") , "验证第"+i+"条记录返回结果中有stockNum这个key");
			  Logger.verifyEquals(true,infoAry.has("topicId") , "验证第"+i+"条记录返回结果中有topicId这个key");
			  Logger.verifyEquals(true,infoAry.has("isCheck") , "验证第"+i+"条记录返回结果中有isCheck这个key");
			  Logger.verifyEquals(true,infoAry.has("showTitle") , "验证第"+i+"条记录返回结果中有showTitle这个key");
			  Logger.verifyEquals(true,infoAry.has("bulkId") , "验证第"+i+"条记录返回结果中有bulkId这个key");
			  
				if (infoAry.has("id") && infoAry.has("productIds") && infoAry.has("stockNum") && 
						infoAry.has("topicId")&& infoAry.has("isCheck") && infoAry.has("showTitle") ) {
					String id = null;
					String buyReason = null;
					String picUrl = null;
					String productIds = null;
					String stockNum = null;
					String isCheck = null;
					String showTitle = null;
					//String onlookers = null;

					if (infoAry.get("id") != "0" && infoAry.get("id") != null) {
						id = infoAry.getString("id");
					}
					
					if (infoAry.get("stockNum") != "0"
							&& infoAry.get("stockNum") != null) {
						stockNum = infoAry.getString("stockNum");
					}
					if (infoAry.get("showTitle") != "0"
							&& infoAry.get("showTitle") != null) {
						showTitle = infoAry.getString("showTitle");
					}
					
					//只要保证有这些键值就行可，前面已经判断过，至于值是否为空测试环境脏数据会导致数据为空
					Logger.verifyNotNull(id, "验证第" + i + "条记录返回结果中有id不为空且不为0");
					/*Logger.verifyNotNull(buyReason, "验证第" + i + "条记录返回结果中有buyReason购买理由不为空且不为0");
					Logger.verifyNotNull(picUrl, "验证第" + i+ "条记录返回结果中有picUrl商品图不为空且不为0");
					Logger.verifyNotNull(productIds, "验证第" + i+ "条记录返回结果中有productIds商品id不为空且不为0");
					Logger.verifyNotNull(stockNum, "验证第" + i+ "条记录返回结果中有stockNum商品数不为空且不为0");
					Logger.verifyNotNull(isCheck, "验证第" + i+ "条记录返回结果中有isCheck不为空且不为0");*/
					Logger.verifyNotNull(showTitle, "验证第" + i+ "条记录返回结果中有showTitle热词标题不为空且不为0");
					//Logger.verifyNotNull(infoAry.getString("onlookers"), "验证第"+ i + "条记录返回结果中有onlookers围观数不为空且不为0");

				}
			
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}
